package com.cmict.data.infra.utils;


import java.sql.Connection;

import com.cmict.data.framework.security.core.util.AESUtil;
import com.cmict.data.framework.security.core.util.RSAUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class DoConnection {
    private static Logger logger = LogManager.getLogger();
    public static Connection connection(String dbtype,String ip,String port,String dbname,String username,String password){
        if(!ip.contains("mysql")){
            password = RSAUtils.decrypt(password);
        }

//        if(password.startsWith(AESUtil.ENCRYPT_FLAG)){
//            password = AESUtil.decrypt(password);
//        }
//    public static Connection connection(String dbtype,String linktype,String tns,String ip,String port,String dbname,String username,String password){
        Connection conn = null;
        try {
            String jdbcurl = "";
//            if ("ORACLE".equals(dbtype.toUpperCase())) {
//                if("单节点".equals(linktype)){
//                    jdbcurl = "jdbc:oracle:thin:@" + ip + ":"
//                            + port + ":" + dbname;
//                }else{
//                    jdbcurl = "jdbc:oracle:thin:@ "+ tns;
//                }
//                conn =DBHelper.initOracle(jdbcurl,username,password, false);
//
//            } else
                if ("MYSQL".equals(dbtype.toUpperCase())) {
                    if(ip.contains("mysql")){
                        jdbcurl = ip;
                    }else{
                        jdbcurl = "jdbc:mysql://" + ip + ":"
                                + port + "/" + dbname
                                + "?useUnicode=true&characterEncoding=utf-8";
                    }

                conn =DBHelper.initMysql(jdbcurl, username,password, false);

            } else if ("DM".equals(dbtype.toUpperCase())) {
                    jdbcurl = "jdbc:dm://" + ip + ":" + port;
                    conn =DBHelper.initDm(jdbcurl, username,password, false);
            } else if ("DB2".equals(dbtype.toUpperCase())) {
                jdbcurl = "jdbc:db2://" + ip + ":"
                        + port + "/" + dbname;
                conn =DBHelper.initDB2(jdbcurl, username,password, false);

            } else if ("SQLSERVER".equals(dbtype.toUpperCase())) {
                jdbcurl = "jdbc:sqlserver://" + ip + ":"
                        + port + ";DatabaseName=" + dbname;
                conn =DBHelper.initSQLServer(jdbcurl, username,password, false);

//            }else if("GREENPLUM".equals(dbtype.toUpperCase())){
//                /*jdbcurl = "jdbc:pivotal:greenplum://" + ip + ":"
//                        + port + ";DatabaseName=" + dbname;*/
//                jdbcurl = tns;
//                conn = DBHelper.initGreenPlum(jdbcurl, username, password, false);
//            }else if("HIVE".equals(dbtype.toUpperCase())){
//                /*jdbcurl = "jdbc:hive2://" + ip + ":"+port + "/default";*/
//                jdbcurl = tns;
//                conn = DBHelper.initHIVE(jdbcurl, username, password, false);
            }else {
                return null;
            }
        } catch (Exception e) {
            logger.error("数据源连接失败",e);
            return null;
        }

        return conn;
    }

    public static void close(Connection conn){
        if (conn != null) {
            DBHelper.closeDB(conn, null, null);
        } else {
            DBHelper.closeDB(conn, null, null);
        }
    }
}
